package sdmakerz_v1.coresyntax.generator;

import com.google.common.base.Objects;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtext.xbase.lib.InputOutput;
import sdmakerz_v1.coresyntax.generator.interfaces.IDiagramRefData;
import sdmakerz_v1.coresyntax.sDMakerV1.Binding;
import sdmakerz_v1.coresyntax.sDMakerV1.Diagram;
import sdmakerz_v1.coresyntax.sDMakerV1.DiagramElement;
import sdmakerz_v1.coresyntax.sDMakerV1.DiagramRef;
import sdmakerz_v1.coresyntax.sDMakerV1.Lifeline;

/**
 * Une classe qui facilite le recuperation les informations
 *  de renommage pour les diagrammes referes.
 *  Il faut savoir que le context contient toujours des donnees
 *  de references. Par exemple quand on genere une declaration d 'objet
 *  a la racine de la description, on demande quand meme au context son
 *  nom reel (meme si celui-ci coincide avec l'ancien). Dans ce cas la,
 *  le context rendra le meme nom (Voir methode init).
 * 
 *  @author pstl-team
 */
@SuppressWarnings("all")
public class DiagramRefDataImpl implements IDiagramRefData {
  private HashMap<String,String> bindings;
  
  private String diagNewName;
  
  /**
   * @param d: le diagramme qui contient la reference
   * @param ref la reference
   * @param refData si on traite deja une reference
   *                on propage les anciennes information de renommage.
   *                C'est refData qui contient ces informations.
   * @param diagNewName, util pour eviter les conflits lors de la creation
   *                     du meme diagramme mais evalue.
   */
  public void init(final Diagram d, final DiagramRef ref, final IDiagramRefData refData, final String diagNewName) {
    HashMap<String,String> _hashMap = new HashMap<String,String>();
    this.bindings = _hashMap;
    this.diagNewName = diagNewName;
    boolean _equals = Objects.equal(ref, null);
    if (_equals) {
      EList<DiagramElement> _diagram_elts = d.getDiagram_elts();
      for (final DiagramElement elt : _diagram_elts) {
        if ((elt instanceof Lifeline)) {
          Lifeline l = ((Lifeline) elt);
          String _name = l.getName();
          String _name_1 = l.getName();
          this.bindings.put(_name, _name_1);
        }
      }
    } else {
      EList<Binding> _bindings = ref.getBindings();
      for (final Binding binding : _bindings) {
        {
          String _to = binding.getTo();
          String _from = binding.getFrom();
          this.bindings.put(_to, _from);
          String _to_1 = binding.getTo();
          String _plus = ("bindings " + _to_1);
          String _plus_1 = (_plus + " with ");
          String _from_1 = binding.getFrom();
          String _plus_2 = (_plus_1 + _from_1);
          InputOutput.<String>println(_plus_2);
        }
      }
    }
    boolean _notEquals = (!Objects.equal(refData, null));
    if (_notEquals) {
      Set<String> _keySet = this.bindings.keySet();
      HashSet<String> _hashSet = new HashSet<String>(_keySet);
      Set<String> keySet = _hashSet;
      for (final String key : keySet) {
        {
          String value = this.bindings.get(key);
          this.bindings.remove(key);
          String _newName = refData.getNewName(value);
          this.bindings.put(key, _newName);
          String _plus = ("finally " + key);
          String _plus_1 = (_plus + " with ");
          String _newName_1 = refData.getNewName(value);
          String _plus_2 = (_plus_1 + _newName_1);
          InputOutput.<String>println(_plus_2);
        }
      }
    }
  }
  
  /**
   * ** GETTERS **
   */
  public String diagNewName() {
    return this.diagNewName;
  }
  
  public String getNewName(final String oldName) {
    String newName = null;
    Set<String> _keySet = this.bindings.keySet();
    for (final String key : _keySet) {
      int _compareTo = key.compareTo(oldName);
      boolean _equals = (_compareTo == 0);
      if (_equals) {
        String _get = this.bindings.get(key);
        newName = _get;
      }
    }
    return newName;
  }
}
